package com.crm.controller;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.crm.model.mdao.ContactMapper;
import com.crm.model.mdao.OutgoodMapper;
import com.crm.model.mdao.OutgooddetailsMapper;
import com.crm.model.mpojo.Outgood;
import com.crm.model.pojo.Orderinfo;
import com.crm.model.pojo.Returnmoney;
import com.crm.services.ContactServices;
import com.crm.services.CustomerServices;
import com.crm.services.OutgoodService;
import com.crm.services.UserServices;
import com.github.pagehelper.PageInfo;

@Controller
public class OutgoodController {
	@Autowired
	OutgoodService outgoodService;
	
	@Autowired
	OutgoodMapper ogMapper;
	
	@Autowired
	OutgooddetailsMapper ogdetailsMapper;
	@Autowired
	CustomerServices customerServices;
	@Autowired
	ContactServices contactServer;
	@Autowired
	UserServices userServer;
	@Autowired
	ContactMapper conMapper;

	/**
	 * 分页跳转到发货单页
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@GetMapping("/sales/prosale/outgood")
	public ModelAndView order(Integer pageNum, Integer pageSize,Integer statusid ,String search) {
		if (pageNum == null)
			pageNum = 1;
		if (pageSize == null)
			pageSize = 5;
		ModelAndView mv = new ModelAndView("sales/prosale/outgood");
		//分页查询订单
		List<Map<String, String>> findAllMaper = outgoodService.selectByPager(pageNum, pageSize,statusid,search);
		//获取发货单数据
		System.out.println(findAllMaper);
		String jsonString = JSONObject.toJSONString(findAllMaper);
		//获取分页信息
		PageInfo<Map<String, String>> pager = new PageInfo<Map<String, String>>(findAllMaper);
		System.out.println(pager);
		String json = JSONObject.toJSONString(pager);
		System.out.println(jsonString);
		mv.addObject("list", jsonString);
		mv.addObject("page", pager);
		mv.addObject("pagejson", json);		
		return mv;
	}
	
	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@PostMapping("/sales/prosale/outgood1")
	@ResponseBody	
	public PageInfo<Map<String, String>> orderPage(Integer pageNum, Integer pageSize,Integer statusid ,String search) {
		if (pageNum == null)
			pageNum = 1;
		if (pageSize == null)
			pageSize = 5;
		List<Map<String, String>> selectAll = outgoodService.selectByPager(pageNum, pageSize,statusid,search);
		PageInfo<Map<String, String>> pager = new PageInfo<Map<String, String>>(selectAll);
		return pager;
	}
	
	
	
	/**
	 * 订单视图添加发货单
	 * @return 
	 */
	@PostMapping("/sales/prosale/addoutGoodsview")
	@ResponseBody
	public Integer addorderitemview(@RequestParam("oid") Integer id) {
		Integer addorderitemview = outgoodService.addorderitemview(id);
		return addorderitemview;
	}
	
	/**
	 * 重定向到订发货单页
	 * @return 
	 * @return 
	 * @return
	 */
	@RequestMapping("/sales/prosale/outGoodsbill")
	public ModelAndView orderView(Integer ogid) {
		ModelAndView mv = new ModelAndView("/sales/prosale/outGoodsbill");
		//查询发货
		Map<String, String> selectByogview = ogMapper.selectByogview(ogid);
		//查询发货详情
		List<Map<String, String>> selectByOgid = ogdetailsMapper.selectByOgid(ogid);
		mv.addObject("outgood", selectByogview);
		mv.addObject("outgooditem", selectByOgid);
		mv.addObject("outgoodit", JSONObject.toJSONString(selectByOgid));
		System.out.println("发货"+selectByogview);
		System.out.println("发货详情"+selectByOgid);
		/*
		//订单详情相关数据
		List<Map<String, String>> selectOrderItemView = orderitemsService.selectOrderItemView(id);
		//查询回款计划
		List<Map<String, String>> bmpMap = bpService.findByoId(id);
		List<Map<String, String>> rmMap = rmService.findByoId(id);
		System.out.println(selectOrderItemView);
		mv.addObject("orderView", selectAllView);
		String json = JSONObject.toJSONString(selectOrderItemView);
		mv.addObject("orderinfo",  JSONObject.toJSONString(orderinfo));
		mv.addObject("bmpMap",  JSONObject.toJSONString(bmpMap));
		mv.addObject("rmMap",  JSONObject.toJSONString(rmMap));
		System.out.println("回款计划："+bmpMap);
		System.out.println("回款记录:"+rmMap);
		mv.addObject("orderItemView", json);
		System.out.println(JSONObject.toJSONString(orderinfo));
		System.out.println("订单视图页");*/
		return mv;
	}
	
	/**
	 * 编辑物流信息发货操作
	 * @param comcustid
	 * @param orderid
	 * @param date1
	 * @param bmpmoney
	 * @param radiosatus
	 * @param uid
	 * @param textcommunication
	 * @return
	 */
	@PostMapping("/sales/prosale/delivergoods")
	@ResponseBody
	public String delivergoods(@RequestParam(name = "ogId", required = false) Integer ogId,
			@RequestParam(name = "oId", required = false) Integer oId,
			@RequestParam(name = "ogRemark", required = false) String ogRemark,
			@RequestParam(name = "ogPhysical", required = false) String ogPhysical,
			@RequestParam(name = "ogPhysicalid", required = false) String ogPhysicalid,
			@RequestParam(name = "carriagemoney", required = false) String carriagemoney,
			@RequestParam(name = "carriagejs", required = false) String carriagejs) {
		Outgood outgood = new Outgood();
		outgood.setOgId(ogId);
		outgood.setOgRemark(ogRemark);
		outgood.setOgPhysical(ogPhysical);
		outgood.setOgPhysicalid(ogPhysicalid);
		if(carriagemoney==null||carriagemoney.trim()=="")
			carriagemoney=0+"";
		outgood.setCarriagemoney(new BigDecimal(carriagemoney));
		outgood.setCarriagejs(carriagejs);
		outgoodService.delivergoods(outgood,oId);
		return "编辑物流信息发货操作";
	}
	
	/**
	 * 编辑物流信息
	 * @param comcustid
	 * @param orderid
	 * @param date1
	 * @param bmpmoney
	 * @param radiosatus
	 * @param uid
	 * @param textcommunication
	 * @return
	 */
	@PostMapping("/sales/prosale/delivergood")
	@ResponseBody
	public String delivergood(@RequestParam(name = "ogId", required = false) Integer ogId,
			@RequestParam(name = "oId", required = false) Integer oId,
			@RequestParam(name = "ogRemark", required = false) String ogRemark,
			@RequestParam(name = "ogPhysical", required = false) String ogPhysical,
			@RequestParam(name = "ogPhysicalid", required = false) String ogPhysicalid,
			@RequestParam(name = "carriagemoney", required = false) String carriagemoney,
			@RequestParam(name = "carriagejs", required = false) String carriagejs) {
		Outgood outgood = new Outgood();
		outgood.setOgId(ogId);
		outgood.setOgRemark(ogRemark);
		outgood.setOgPhysical(ogPhysical);
		outgood.setOgPhysicalid(ogPhysicalid);
		outgood.setCarriagemoney(new BigDecimal(carriagemoney));
		outgood.setCarriagejs(carriagejs);
		outgoodService.delivergood(outgood);
		return "编辑物流信息";
	}
	
	/**
	 * 用于查询加载订单视图修改订单页的界面数据
	 * @return
	 */
	@PostMapping("/sales/prosale/updatefaaddpage")
	@ResponseBody
	public JSONObject updateOrderpage(@RequestParam(name = "ID", required = false) Integer custId) {
		System.out.println("是什么"+custId);
		// 用于加载客户信息
		List<Map<String, String>> selectAllCustomer = customerServices.selectAllCustomer();
		//所有联系人信息
		List<Map<String, String>> selectContactAll = conMapper.selectConbycustId(custId);
		//所有者信息
		List<Map<String, String>> selectUserAll = userServer.selectUserAll();
		JSONObject jsonarray=new JSONObject();
		jsonarray.put("customer", selectAllCustomer);
		jsonarray.put("contact", selectContactAll);
		jsonarray.put("userAll", selectUserAll);
		return jsonarray;
	}
	
	/**
	 * 编辑发货单订单
	 * @param comcustid
	 * @param orderid
	 * @param date1
	 * @param bmpmoney
	 * @param radiosatus
	 * @param uid
	 * @param textcommunication
	 * @return
	 */
	@PostMapping("/sales/prosale/updateAddress")
	@ResponseBody
	public String updateAddress(@RequestParam(name = "ogid", required = false) Integer ogId,
			@RequestParam(name = "uid", required = false) Integer uid,
			@RequestParam(name = "postcode", required = false) String ogPostcode,
			@RequestParam(name = "contaddress", required = false) String ogAddress,
			@RequestParam(name = "oconsignee", required = false) String consigner,
			@RequestParam(name = "conphone", required = false) String ogPhone) {
		Outgood outgood = new Outgood();
		outgood.setOgId(ogId);
		outgood.setConsigner(uid+"");
		outgood.setOgPostcode(ogPostcode);
		outgood.setOgAddress(ogAddress);
		outgood.setShoumen(consigner);
		outgood.setOgPhone(ogPhone);
		outgoodService.delivergoodAddress(outgood);
		return "编辑物流信息";
	}
}
